package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.TbContentCategoryService;
import com.dongdongshop.service.TbContentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("content")
public class TbContentController {

    @Reference
    private TbContentService tbContentService;

    @Reference
    private TbContentCategoryService tbContentCategoryService;

    @RequestMapping("list")
    public String list(Model model){
        List<TbContentCategory> category = tbContentCategoryService.getCategoryList();
        model.addAttribute("category",category);
        return "/admin/content";
    }

    @RequestMapping("getContentList")
    @ResponseBody
    public Result getContentList(){
        List<TbContent> list = tbContentService.getContentList();
        return Result.build(ConstantEnum.SUCCESS).setRows(list);
    }

    @RequestMapping("insertContentInfo")
    @ResponseBody
    public Result insertContentInfo(TbContent tbContent){
        tbContentService.insertContentInfo(tbContent);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("updateContent")
    @ResponseBody
    public Result updateContent(Long id){
        TbContent content = tbContentService.updateContent(id);
        return Result.build(ConstantEnum.SUCCESS).setResult(content);
    }

    @RequestMapping("updateContentInfo")
    @ResponseBody
    public Result updateContentInfo(TbContent tbContent){
        tbContentService.updateContentInfo(tbContent);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("deleteContent")
    @ResponseBody
    public Result deleteContent(Long id){
        tbContentService.deleteContent(id);
        return Result.build(ConstantEnum.SUCCESS);
    }

}
